package com.centit.framework.ip.businessdata.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.centit.framework.common.JsonResultUtils;
import com.centit.framework.common.ResponseMapData;
import com.centit.framework.components.CodeRepositoryUtil;
import com.centit.framework.core.controller.BaseController;
import com.centit.framework.core.dao.CodeBook;
import com.centit.framework.ip.businessdata.service.TodoItemManager;
import com.centit.framework.ip.util.layuiResultUtil;
import com.centit.framework.security.model.CentitUserDetails;
import com.centit.support.algorithm.StringBaseOpt;
import com.centit.support.database.utils.PageDesc;
import com.centit.support.database.utils.QueryAndNamedParams;
import com.centit.support.database.utils.QueryUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * NbbgTxl  Controller.
 * create by scaffold 2018-06-27 
 * @author codefan@sina.com
 * 通讯录【T_NBBG_TXL】null   
*/


@Controller
@RequestMapping("/todoItem")
public class TodoItemController extends BaseController {
	private static final Log log = LogFactory.getLog(TodoItemController.class);
	
	@Resource
	private TodoItemManager todoItemMag;

    /**
     * 查询所有   通讯录【T_TODO_ITEM】  列表
     *
     * @param request  {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     * @return {data:[]}
     */
    @RequestMapping(value = "/listTodoItem",method = RequestMethod.GET)
    public void listTodoItem(String[] field, HttpServletRequest request, HttpServletResponse response) {
        try {
            request.getRequestDispatcher("/modules/todoitem/moretodoitem.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(method = RequestMethod.GET)
    public void list(String[] field,PageDesc pageDesc, HttpServletRequest request, HttpServletResponse response) {
        try {
            //解析参数信息
            Map<String, Object> searchColumn = convertSearchColumn(request);
            String limit=request.getParameter("limit");//必要写法
            String page=request.getParameter("page");//必要写法
            pageDesc.setPageSize(Integer.parseInt(limit));//设置参数
            pageDesc.setPageNo(Integer.parseInt(page));
            searchColumn.put("isValid","T");
            searchColumn.put("userCode",getLoginUserCode(request));
            String itemState=(String)searchColumn.get("itemState1");
            if(StringUtils.isNotBlank(itemState)){
                searchColumn.put("itemState",itemState.split(","));
            }
            searchColumn.put(CodeBook.SELF_ORDER_BY, "createTime desc");
            JSONArray jsonArrayObjects = todoItemMag.listObjectsAsJson(searchColumn, pageDesc);
            //翻译字典项
            if (jsonArrayObjects.size() > 0) {
                for (int i = 0; i < jsonArrayObjects.size(); i++) {
                    JSONObject jsonObject1 = jsonArrayObjects.getJSONObject(i);
                    //翻译字典项
                    if (!StringBaseOpt.isNvl(jsonObject1.getString("systemId"))) {
                        jsonObject1.put("systemName", getDicValue(jsonObject1.getString("systemId"), "ptbmxx"));
                    }
                    //翻译待办类型
                    if (!StringBaseOpt.isNvl(jsonObject1.getString("itemState"))) {
                        jsonObject1.put("itemType", getDicValue(jsonObject1.getString("itemState"), "ITEMSTATE"));
                    }

                }

            }

            if (null == pageDesc) {
                JsonResultUtils.writeSingleDataJson(jsonArrayObjects, response);
                return;
            }
            ResponseMapData resData = new ResponseMapData();
            resData.addResponseData(OBJLIST, jsonArrayObjects);
            resData.addResponseData(PAGE_DESC, pageDesc);
            layuiResultUtil.layuiwriteResponseDataAsJson(resData, pageDesc, response);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

    }
    @RequestMapping(value = "/loadDbinfo",method = RequestMethod.GET)
    public void  loadDbinfo(HttpServletRequest request,HttpServletResponse response){
        Map<String, Object> filterMap=new HashMap<String, Object>();
        CentitUserDetails userDetails =getLoginUser(request);
        String sql=" select itemState,count(1) from  T_TODO_ITEM  where isValid='T' [:USER_CODE| and USER_CODE=:USER_CODE] and itemState is not null  group by itemState";
        filterMap.put("USER_CODE",userDetails.getUserCode());
        QueryAndNamedParams params = QueryUtils.translateQuery(sql, filterMap);
        JSONArray jsonArray=todoItemMag.listObjectsBySqlAsJson(params.getQuery(),params.getParams(),null);
        ResponseMapData resData = new ResponseMapData();
        resData.addResponseData(OBJLIST, jsonArray);
        JsonResultUtils.writeResponseDataAsJson(resData, response);
    }


    // 翻译字典项
    private String getDicValue(String code, String type) {
        return CodeRepositoryUtil.getValue(type,code);
    }


    }
